import logging
import math
from typing import Optional, List

from fastapi import APIRouter, Depends, Query, Path
from pydantic import Field, BaseModel
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from commons.BaseResponse import BaseResponse
from commons.PaginationBase import PaginationBase
from datebase import get_db
from utils.EncryptionUtil import oauth2_scheme, EncryptionMiddleware

attribute_router = APIRouter(prefix="/attributeValue", tags=["危废数据字典查询接口"],
                             dependencies=[Depends(oauth2_scheme)])


class AttributeSearch(BaseModel):
    itemId: str = Field(..., description="数据字典id")


@attribute_router.get("/list")
def list_attribute(search: AttributeSearch = Query(), db: Session = Depends(get_db)):
    sql = "select attribute_value_id,attribute_value,attribute_code"
    sql = sql + " from w_attribute_value where fk_attribute_item_id= :itemId "
    sql = sql + " order by  attribute_value_id,attribute_code;"

    data_2000 = db.execute(text(sql), {
        "itemId": search.itemId,
    }).fetchall()

    data_list = []
    for key in range(len(data_2000)):
        d1 = {}
        d1['attribute_value_id'] = data_2000[key][0]
        d1['attribute_value'] = data_2000[key][1]
        d1['attribute_code'] = data_2000[key][2]
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)
